package com.reactim.user.controller;

import com.reactim.user.entity.UserPrivacySetting;
import com.reactim.user.service.UserPrivacyService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * 用户隐私设置控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/privacy")
@RequiredArgsConstructor
public class UserPrivacyController {
    
    private final UserPrivacyService userPrivacyService;
    
    /**
     * 获取用户隐私设置
     */
    @GetMapping("/{userId}")
    public ResponseEntity<UserPrivacyResponse> getUserPrivacySetting(@PathVariable Long userId) {
        UserPrivacySetting setting = userPrivacyService.getUserPrivacySetting(userId);
        UserPrivacyResponse response = convertToResponse(setting);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 更新用户隐私设置
     */
    @PutMapping("/{userId}")
    public ResponseEntity<UserPrivacyResponse> updatePrivacySetting(
            @PathVariable Long userId,
            @Valid @RequestBody UpdatePrivacySettingDto request) {
        
        UserPrivacySetting setting = new UserPrivacySetting();
        setting.setFriendRequestPermission(request.getFriendRequestPermission());
        setting.setRequireVerification(request.getRequireVerification());
        setting.setAllowSearchByPhone(request.getAllowSearchByPhone());
        setting.setAllowSearchByEmail(request.getAllowSearchByEmail());
        setting.setAllowSearchByUsername(request.getAllowSearchByUsername());
        setting.setShowOnlineStatus(request.getShowOnlineStatus());
        setting.setAllowGroupInvite(request.getAllowGroupInvite());
        
        UserPrivacySetting updatedSetting = userPrivacyService.updatePrivacySetting(userId, setting);
        UserPrivacyResponse response = convertToResponse(updatedSetting);
        
        log.info("更新用户隐私设置成功: userId={}", userId);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 重置为默认隐私设置
     */
    @PostMapping("/{userId}/reset")
    public ResponseEntity<UserPrivacyResponse> resetToDefault(@PathVariable Long userId) {
        UserPrivacySetting setting = userPrivacyService.resetToDefault(userId);
        UserPrivacyResponse response = convertToResponse(setting);
        
        log.info("重置用户隐私设置为默认: userId={}", userId);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 检查用户是否允许被搜索
     */
    @GetMapping("/{userId}/can-be-searched")
    public ResponseEntity<CanBeSearchedResponse> canBeSearched(
            @PathVariable Long userId,
            @RequestParam String searchType,
            @RequestParam String searchValue) {
        
        boolean canBeSearched = userPrivacyService.canBeSearched(userId, searchType, searchValue);
        
        CanBeSearchedResponse response = new CanBeSearchedResponse();
        response.setCanBeSearched(canBeSearched);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 检查用户是否允许接收好友请求
     */
    @GetMapping("/{toUserId}/can-receive-friend-request")
    public ResponseEntity<CanReceiveFriendRequestResponse> canReceiveFriendRequest(
            @PathVariable Long toUserId,
            @RequestParam Long fromUserId) {
        
        boolean canReceive = userPrivacyService.canReceiveFriendRequest(fromUserId, toUserId);
        
        CanReceiveFriendRequestResponse response = new CanReceiveFriendRequestResponse();
        response.setCanReceive(canReceive);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 转换为响应对象
     */
    private UserPrivacyResponse convertToResponse(UserPrivacySetting setting) {
        UserPrivacyResponse response = new UserPrivacyResponse();
        response.setId(setting.getId());
        response.setUserId(setting.getUserId());
        response.setFriendRequestPermission(setting.getFriendRequestPermission());
        response.setRequireVerification(setting.getRequireVerification());
        response.setAllowSearchByPhone(setting.getAllowSearchByPhone());
        response.setAllowSearchByEmail(setting.getAllowSearchByEmail());
        response.setAllowSearchByUsername(setting.getAllowSearchByUsername());
        response.setShowOnlineStatus(setting.getShowOnlineStatus());
        response.setAllowGroupInvite(setting.getAllowGroupInvite());
        response.setCreatedAt(setting.getCreatedAt());
        response.setUpdatedAt(setting.getUpdatedAt());
        return response;
    }
    
    // DTO classes
    
    @Data
    public static class UpdatePrivacySettingDto {
        private UserPrivacySetting.FriendRequestPermission friendRequestPermission;
        private Boolean requireVerification;
        private Boolean allowSearchByPhone;
        private Boolean allowSearchByEmail;
        private Boolean allowSearchByUsername;
        private Boolean showOnlineStatus;
        private Boolean allowGroupInvite;
    }
    
    @Data
    public static class UserPrivacyResponse {
        private Long id;
        private Long userId;
        private UserPrivacySetting.FriendRequestPermission friendRequestPermission;
        private Boolean requireVerification;
        private Boolean allowSearchByPhone;
        private Boolean allowSearchByEmail;
        private Boolean allowSearchByUsername;
        private Boolean showOnlineStatus;
        private Boolean allowGroupInvite;
        private LocalDateTime createdAt;
        private LocalDateTime updatedAt;
    }
    
    @Data
    public static class CanBeSearchedResponse {
        private boolean canBeSearched;
    }
    
    @Data
    public static class CanReceiveFriendRequestResponse {
        private boolean canReceive;
    }
}